import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Home from '../components/home.vue'
import Users from '../components/system/user/index.vue'

Vue.use(VueRouter)
// 路由规则
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home, children:[
      { path: '/system/user/index', component: Users}
    ] }
]

const router = new VueRouter({
  routes
})

//路由导航守卫
router.beforeEach((to, from, next) => {
  //to表示将要访问的路径
  //from表示从哪个路径跳转而来
  //next表示一个函数，放行：next() 强制跳转next("/login")
  if(to.path == '/login'){
     return next();
  }else{
    const token = window.sessionStorage.getItem("token");
    // const token = "";
    if(!token){
      return next("/login");
     }
  }
  next()
})

export default router
